package yxy.game.pm2.chat.command.player;

import yxy.apple.util.UnicodeUtility;
import yxy.cherry.data.dfa.Sensitive;
import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Nickname;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.CurrencyHandle;

/** 玩家 修改玩家名字 */
@MessageType(proto = Command.PlayerNameChange, classtype = PlayerMessage.PlayerNameChangeRequest.class)
public class CMD_PlayerNameChange extends Command<PlayerMessage.PlayerNameChangeRequest> {

	@Override
	public void execute(MessageSource messageSource, PlayerMessage.PlayerNameChangeRequest request) {
		String playerCode = messageSource.getTag();
		String nickname = request.getNickname();

		PlayerMessage.PlayerNameChangeResponse.Builder response = PlayerMessage.PlayerNameChangeResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		Nickname nicknameHandle = Nickname.getInstance();

		try {
			if (playerBean.tryLock() && nicknameHandle.tryLock()) {
				// 字符整形 去掉空格 全角改半角
				nickname = nicknameHandle.checkName(nickname);
				if (nickname == null || nickname.length() <= 0) {
					send(messageSource, response, Result.PlayerNameNotStandard);// 名字不太合适
					return;
				}
				if (!UnicodeUtility.checkUnicode(nickname, UnicodeUtility.UnicodeSet_Name)) {
					send(messageSource, response, Result.PlayerNameNotStandard);// 名字不太合适
					return;
				}
				// 不能和code一样
				if (nickname.equals(playerCode)) {
					send(messageSource, response, Result.PlayerNameNotStandard);// 名字不太合适
					return;
				}
				// 长度检查
				if (nickname.length() > 6) {
					send(messageSource, response, Result.PlayerNameLengthTooLong);// 名字太长了
					return;
				}
				// 检查名字是否用过
				boolean isuse = nicknameHandle.checkNameIsUsed(nickname);
				if (isuse) {
					send(messageSource, response, Result.PlayerNameUsed);// 名字已经被用了
					return;
				}
				// 敏感词检查
				if (Sensitive.isSensitive(nickname)) {
					send(messageSource, response, Result.PlayerNameNotStandard);// 名字不太合适
					return;
				}

				if (playerBean.getName().equals(playerBean.getCode())) {
					// 初次改名 免费
				} else {
					int costnum = fomula.改名花费元宝个数.intValue();
					Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costnum);
					try {
						new CurrencyHandle().cost(playerCode, CCReason.角色改名, cost);
					} catch (CurrencyNotEnoughException e) {
						send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
						return;
					}
				}

//				String oldname = playerBean.getName();

				playerBean.setName(nickname);
				playerBean.set();
				nicknameHandle.set(playerBean);
//				nicknameHandle.remove(oldname);

				response.setNickname(playerBean.getName());
				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			playerBean.unlock();
			nicknameHandle.unlock();
		}

	}

	private void send(MessageSource messageSource, PlayerMessage.PlayerNameChangeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}